package week10;

public class IntNodeList{
    private IntNode list;

    public IntNodeList() {
        list = null;
    }

    public void add(int i) {
        IntNode node = new IntNode(i);
        IntNode current;

        if (list == null)
        {list = node;}
        else {
            current = list;
            while (current.next != null)
            {current = current.next;}
            current.next = node;
        }
    }

    public IntNode getList(){
        return list;
    }

    private class IntNode {
        public int anInt;
        public IntNode next;

    //--------------------------------------------------------------
    //  Sets up the node
    //--------------------------------------------------------------

        public IntNode(int i) {
            anInt = i;
            next = null;
        }
    }

    public void selectSort(){
        int temp;
        if (list.anInt > list.next.anInt) {
            temp = list.next.anInt;
            list.next.anInt = list.anInt;
            list.anInt = temp;
        }
        IntNode temp1 = list;
        IntNode temp2;

        while (temp1.next != null) {
            temp2 = temp1.next;
            while (temp2.next != null) {
                if (temp1.next.anInt > temp2.next.anInt) {
                    temp = temp2.next.anInt;
                    temp2.next.anInt = temp1.next.anInt;
                    temp1.next.anInt = temp;
                }
                temp2 = temp2.next;
            }
            temp1 = temp1.next;
        }
    }

    @Override
    public String toString() {
        String result = "";

        IntNode current = list;

        while (current != null) {
            result += current.anInt + "\n";
            current = current.next;
        }

        return result;
    }

}
